/*
 * @file window_data.h
 */

#ifndef WINDOW_DATA_H
#define WINDOW_DATA_H

#include "gkt_types.h"

#ifdef __cplusplus
extern "C" {
#endif

#include "gkt_door.h"

#if defined(GKT_CONFIG_NETWORK_ENABLE) && GKT_CONFIG_NETWORK_ENABLE
#include "gkt_network.h"
#include "gkt_date_time.h"
#endif



#define WINDOW_DATA_ALIGNMENT	(sizeof(int) * 2)

/************************************************************
 * WINDOW UNRESET DATA
 ************************************************************/

struct _window_unreset_data {
#if defined(GKT_CONFIG_DOOR_LOCK_ENABLE) && GKT_CONFIG_DOOR_LOCK_ENABLE
#if !GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FIXED
	int	lock_motor_rotate_direction_set;
	uint32_t	lock_motor_rotate_direction;
#endif
#if !GKT_DOOR_LOCK_MOTOR_ROTATE_TIME_FIXED
	uint32_t	lock_motor_rotate_time_ms;
#endif
#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC
	uint32_t	lock_body_type;
	uint32_t	lock_motor_torsion_gear;
	uint32_t	lock_motor_full_rotate_time_ms;
#if (GKT_MODULE_LOC_FRONTBOARD == GKT_DOOR_LOCK_LOCATION)
	uint16_t	lock_motor_locked_rotor_voltage[GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_MAX][GKT_DOOR_LOCK_ROTOR_VOLTAGE_MAX];	/* unit in mV */
#endif
	int	lock_motor_self_learn_complete;
#endif
#endif
	int	adc_adjust_value;
	uint32_t keypad_parameter_value;
	uint32_t rfid_standby_mode;
	int factory_inspect_complete;
	
#if defined(GKT_CONFIG_RFID_ENABLE) && GKT_CONFIG_RFID_ENABLE	\
	&& defined (GKT_CONFIG_RFID_YC5018) && GKT_CONFIG_RFID_YC5018
	int yc_rfid_water_level_factory;
	uint32_t yc_rfid_gain;
#endif
	uint32_t	aging_test_cnt;
#if defined(GKT_CONFIG_DOOR_TRIAL_MODE_ENABLE) && GKT_CONFIG_DOOR_TRIAL_MODE_ENABLE
	uint16_t	trial_auth_mode;
	uint16_t	trial_auth_count_max;
	uint16_t	trial_auth_count_min;
	uint16_t	trial_auth_counter;
#endif
#if defined(GKT_CONFIG_DOOR_VERIFY_LOCKED_ENABLE) && GKT_CONFIG_DOOR_VERIFY_LOCKED_ENABLE
	uint32_t verify_locked_status;
#endif
} __ALIGNED(WINDOW_DATA_ALIGNMENT);
typedef struct _window_unreset_data	window_unreset_data_s;
extern window_unreset_data_s *gp_window_unreset_data;

#if defined(GKT_CONFIG_DOOR_LOCK_ENABLE) && GKT_CONFIG_DOOR_LOCK_ENABLE \
	&& defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC
#define window_door_lock_body_is_normal()	\
	((GKT_DOOR_LOCK_BODY_TYPE_NORMAL == gp_window_unreset_data->lock_body_type) ? 1 : 0)
#endif

/************************************************************
 * WINDOW DATA
 ************************************************************/
struct _window_data {
#if defined(GKT_CONFIG_MULTIPLE_LANGUAGE_SUPPORT) \
	&& GKT_CONFIG_MULTIPLE_LANGUAGE_SUPPORT
	int	language;
#endif

	uint32_t	voice_volume;
	int	voice_mute_enable;

	uint32_t	lock_auto_close_time;	/* default: unreset_data.auto_close_time_default */
#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC
	uint32_t	lock_auto_double_close_time;/* default:0 - inexecution double_close*/
#endif
	uint32_t	guest_access_time;	/* default: 00:00 ~ 24:00 -> 0x00001800  */

	int	dual_verify_enable;		/* default: 0 - disabled */
	int	door_tamper_alarm_enable;	/* default: 0 - disabled */

#if (defined(GKT_CONFIG_PROXIMITY_SENSOR_ENABLE) && GKT_CONFIG_PROXIMITY_SENSOR_ENABLE)
	uint32_t pr_sensor_state;		/* PR_SENSOR_STATE_E */
#endif
} __ALIGNED(WINDOW_DATA_ALIGNMENT);
typedef struct _window_data	window_data_s;
extern window_data_s *gp_window_data;

/***********************************************************
 * WINDOW RETENTION MEM DATA
 ***********************************************************/
typedef struct _window_retmem_data {
	int	always_open_enabled;

	uint32_t	verify_failed_counts;
	uint32_t	verify_failed_expire_dtu;
	uint32_t	verify_locked_clear_dtu;

#if defined(GKT_CONFIG_NETWORK_ENABLE) && GKT_CONFIG_NETWORK_ENABLE
#if 0	
#if (GKT_NETWORK_SYNC_CYCLE >= GKT_NETWOTK_TIME_SYNC_DAILY)
	gkt_date_u	network_time_sync_date;
#endif
	gkt_date_time_s	network_remote_unlock_req_dt;
#endif	
	int	wait_remote_unlock_response;
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
	uint32_t	face_id_nomatch_cnt;
	uint32_t	face_id_nomatch_clear_dtu;	
#endif
#endif
#if	defined(GKT_CONFIG_PROXIMITY_SENSOR_ENABLE) && (GKT_CONFIG_PROXIMITY_SENSOR_ENABLE)
	uint16_t	pr_power_state;//APP USED
	uint16_t	pr_power_cnt;
	uint32_t	pr_power_clear_dtu;
#endif
} window_retmem_data_s;
extern window_retmem_data_s g_window_retmem_data;

/************************************************************
 * WINDOW RAM DATA
 ************************************************************/
typedef struct _window_ram_data {
	uint32_t	login_admin_user_index;
#if defined(GKT_CONFIG_RFID_ENABLE) && GKT_CONFIG_RFID_ENABLE
	uint32_t	delay_unmask_card;
	uint32_t	delay_unmask_card_ms;
#endif

	int	door_tamper_detect;
	int	door_tamper_alarm;

	int	factory_reset_finish;
	int	opcode_state;

#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
	uint32_t	face_id_verify_suc_state;
	uint32_t	face_id_power_state;
	uint32_t	face_id_init_state;
	uint32_t	face_id_demomode_state;
	uint32_t	silent_mode;
#endif
} window_ram_data_s;
extern window_ram_data_s g_window_ram_data;

#define WINDOW_DATA_DELAY_UNMASK_CARD_TIME	3000
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
enum {
	WINDOW_DATA_FACE_ID_STATE_INIT = 0,
	WINDOW_DATA_FACE_ID_STATE_SET,
	WINDOW_DATA_FACE_ID_STATE_MAX
};
#endif

int window_data_init(void);
int window_data_save(void);
void window_data_reset(void);

#ifdef __cplusplus
}
#endif

#endif	/* GKT_WINDOW_SYSTEM_DATA_H */

